//
//  DateUtil.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-24.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "DateUtil.h"

@implementation DateUtil

+(NSString *)getCurrentTimeStr
{
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    date = [formatter stringFromDate:[NSDate date]];
    return date;
}

+(NSString *)getTimeStr:(NSDate *)_date
{
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    date = [formatter stringFromDate:_date];
    return date;
}

+(NSString *)getCurrentYMDStr:(NSDate *)_date
{
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    date = [formatter stringFromDate:_date];
    return date;
}

+(NSString *)getCurrentYMStr:(NSDate *)_date
{
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM"];
    date = [formatter stringFromDate:_date];
    return date;
}

+(int)numDaysInMonth:(NSDate*)_date{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSRange rng = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:_date];
    NSUInteger numberOfDaysInMonth = rng.length;
    return numberOfDaysInMonth;
}

+(NSString *)getDoubleDateStrByInt:(int)date
{
    if (date>0&&date<10) {
        return [NSString stringWithFormat:@"0%i",date];
    }
    return [NSString stringWithFormat:@"%i",date];
}

+(NSDate *)convertStr2Date:(NSString *)timestr format:(NSString *)format
{
    NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
    [inputFormatter setDateFormat:format];
    NSDate* inputDate = [inputFormatter dateFromString:timestr];
    return inputDate;
}

+(NSString *)convertDate2FormatStr:(NSDate *)_date format:(NSString *)format
{
    NSString* date;
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:format];
    date = [formatter stringFromDate:_date];
    return date;
}

@end
